﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Models.PreFinalAccountFileModel;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManageSystemApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class PreFinalAccountFileController : Controller
    {
        readonly IPreFinalAccountFileServices _perFinalAccountFileServices;
        readonly IUserServices _userServices;
        public PreFinalAccountFileController(IPreFinalAccountFileServices perFinalAccountFileServices,
            IUserServices userServices)
        {
            _perFinalAccountFileServices = perFinalAccountFileServices;
            _userServices = userServices;
        }

        [HttpPost, Route("SaveFile")]
        public ApiResult SaveFile(IFormFile file)
        {
            if (file == null || file.Length <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            return ApiResultStatus.Create(
                _perFinalAccountFileServices.SaveFile(file, out UploadPreFinalAccountFile fileInfo),
                ApiResultCodes.OPERATION_FAILED, fileInfo);
        }

        [HttpPost, Route("Add")]
        public ApiResult Add([FromHeader(Name = "Authorization")] string auth, [FromBody] AddPreFinalAccountFile addData)
        {
            if (addData == null
                || string.IsNullOrWhiteSpace(addData.Name)
                || string.IsNullOrWhiteSpace(addData.FilePath))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_userServices.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            return ApiResultStatus.Create(_perFinalAccountFileServices.Add(addData, userId), ApiResultCodes.OPERATION_FAILED);
        }

        [HttpPost, Route("Upd")]
        public ApiResult Upd([FromBody] UpdPreFinalAccountFile updData)
        {
            if (updData == null
                || string.IsNullOrWhiteSpace(updData.Name)
                || string.IsNullOrWhiteSpace(updData.FilePath))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            return ApiResultStatus.Create(_perFinalAccountFileServices.Upd(updData), ApiResultCodes.OPERATION_FAILED);
        }

        [HttpPost, Route("Del")]
        public ApiResult Del(uint id) 
        {
            if (id <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            return ApiResultStatus.Create(_perFinalAccountFileServices.Del(id), ApiResultCodes.OPERATION_FAILED);
        }

        [HttpPost, Route("Get")]
        public ApiResult Get([FromBody] PageBo<PreFinalAccountFileParams> pageBo)
        {
            if (pageBo == null)
            {
                pageBo = new PageBo<PreFinalAccountFileParams>();
            }
            if (pageBo.Param == null)
            {
                pageBo.Param = new PreFinalAccountFileParams();
            }
            if (pageBo.Size <= 0)
            {
                pageBo.Size = 10;
            }
            if (pageBo.Page <= 0)
            {
                pageBo.Page = 1;
            }
            return ApiResultStatus.Success(_perFinalAccountFileServices.Get(pageBo));
        }

        [HttpPost, Route("Audit")]
        public ApiResult Audit([FromHeader(Name = "Authorization")] string auth, [FromBody] PreFinalAccountFileAudit audit)
        {
            if (!_userServices.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (audit == null || audit.Id <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_perFinalAccountFileServices.IsWaitingAudit(audit.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_EXIST);
            }
            return ApiResultStatus.Create(_perFinalAccountFileServices.Audit(audit, userId), ApiResultCodes.OPERATION_FAILED);
        }

        /// <summary>
        /// 手机端，获取自己的
        /// </summary>
        /// <param name="pageBo"></param>
        /// <returns></returns>
        [HttpPost, Route("GetMyUploadFile")]
        public ApiResult GetMyUploadFile([FromHeader(Name = "Authorization")] string auth, 
            [FromBody] PageBo<PreFinalAccountFileParams> pageBo) 
        {
            if (pageBo == null) 
            {
                pageBo = new PageBo<PreFinalAccountFileParams>();
            }
            if (pageBo.Param == null) 
            {
                pageBo.Param = new PreFinalAccountFileParams();
            }
            if (pageBo.Size <= 0)
            {
                pageBo.Size = 10;
            }
            if (pageBo.Page <= 0)
            {
                pageBo.Page = 1;
            }
            if (!_userServices.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            pageBo.Param.CreateUser = userId;
            return ApiResultStatus.Success(_perFinalAccountFileServices.Get(pageBo));
        }

    }
}
